<?php

if( $_GET[ "social" ] ){
	define( "MEDIA", ROOT . ACCOUNT ."/www/DB/social/" );
} else {
	define( "MEDIA", ROOT . ACCOUNT ."/www/DB/media/" );
}

$id     = (int) $_GET[ 'id' ];
$sze	= ($_GET['sze'])? $DB->simplestring( $_GET['sze'] ): "78.20";
$format = explode( '.' , $sze );
$mask   = (bool) $_GET[ 'msk' ];
$layer  = (bool) $_GET[ 'lay' ];
$untag  = (bool) $_GET[ 'unt' ];

$sizes  = array();

$source = file_exists( MEDIA."$id.jpg" )? MEDIA."$id.jpg": ( file_exists( MEDIA."$id.png" )? MEDIA."$id.png": false );
$o_ext	= substr( $source, -3 );
$ext    = $mask? "png": (( $o_ext == "jpg" )? "jpeg" : "png" );

$file   = $source? MEDIA."$sze/$id.$ext" : MEDIA."$sze/0.png";



// WORK IT, BABY!
header( "Content-type: image/$ext" );

if( file_exists( $file )) readfile( $file );
else {

#	Calculate new size
	$source      = ( $o_ext == "png" )? imagecreatefrompng( $source ) : imagecreatefromjpeg( $source );
	$sizes['ow'] = imagesx( $source );
	$sizes['oh'] = imagesy( $source );
	
	
	
	if( !$format[0] ) $format[0] =  $sizes['ow'] * ($format[1]/$sizes['oh']);
	if( !$format[1] ) $format[1] =  $sizes['oh'] * ($format[0]/$sizes['ow']);
	if( $format[0]/$sizes['ow'] >= $format[1]/$sizes['oh'] ){
				 $scale = $format[0]/$sizes['ow'];
				 $sizes['sw'] = $sizes['ow'];
				 $sizes['sh'] = $format[1]/$scale;
	} else {
				 $scale = $format[1]/$sizes['oh'];
				 $sizes['sh'] = $sizes['oh'];
				 $sizes['sw'] = $format[0]/$scale;
	}
	$sizes['sx'] = ($sizes['ow']-$sizes['sw'])/2;
	$sizes['sy'] = ($sizes['oh']-$sizes['sh'])/3;
	
	$image       = imagecreatetruecolor( $format[0], $format[1] );

	if( !$mask && $o_ext == "png" ) {
		imagesavealpha( $source, 1 );
		imagealphablending( $source, 0 );
		
		imagesavealpha( $image, 1 );
		imagealphablending( $image, 0 );
	}

#	Masked here
	if( $mask && $mask = imagecreatefrompng( MEDIA."$sze/msk.png" )){
	
		imagealphablending( $mask, false );
		imagesavealpha( $mask, true );
		
		imagesavealpha( $image, true );
		imagealphablending($image, false);
		$tlo = imagecolorallocatealpha($image, 255, 255, 255, 127);
		imagefill($image, 255, 255, $tlo);
		imagecopyresampled($image,$source, 0, 0, $sizes['sx'], $sizes['sy'], $format[0], $format[1], $sizes['sw'], $sizes['sh']);
		
		
		for($ax=0; $ax<$format[0]; $ax++){
			for($ay=0; $ay<$format[1]; $ay++){
				$rgba =imagecolorat($mask, $ax, $ay);
				$alpha=($rgba & 0x7F000000) >> 24;
				$alpha=$alpha;
				if($alpha>5){
					if($alpha<125){
						$rgbi = imagecolorsforindex($image, imagecolorat($image, $ax, $ay));
						$coli = imagecolorallocatealpha($image, $rgbi["red"], $rgbi["green"], $rgbi["blue"], $alpha); 
					} else {
						$coli = imagecolorallocatealpha($image, 255, 255, 255, 127); 
					}
					imagesetpixel($image, $ax,$ay, $coli);
				}
				
			}
		}
		
		imagedestroy($mask);


#	... or Layered here	
	} else if( $layer && $layer = imagecreatefrompng( MEDIA."$sze/layer.png" )){
	
		imagealphablending($layer, 1);
		imagecopyresampled($image,$source, 0, 0, $sizes['sx'], $sizes['sy'], $format[0], $format[1], $sizes['sw'], $sizes['sh']);
		
		imagealphablending($image, 1);
		imagecopy($image, $layer, 0,0,0,0, $format[0], 20);
		
		imagedestroy($layer);

#	... or Slightly recropped for border removal here		
	} else if( $untag ) {
		
		imagecopyresampled($image,$source, 0, 0, $sizes['sx'] + ( $sizes['sw']*0.02), $sizes['sy'] + ( $sizes['sh']*0.02), $format[0], $format[1], $sizes['sw']*0.96, $sizes['sh']*0.96);
		

#	... or just Resized.
	} else imagecopyresampled($image,$source, 0, 0, $sizes['sx'], $sizes['sy'], $format[0], $format[1], $sizes['sw'], $sizes['sh'] );
	
#	Check for cache-dir existence
	if( !file_exists( MEDIA ."$sze" )) mkdir( MEDIA ."$sze" );

#	... and save!	
	if( $ext == "png" ) {
		imagepng( $image, $file );
		imagepng( $image, null );
	
	} else {
		imagejpeg( $image, $file, 90 );
		imagejpeg( $image, null, 90 );
	}
	
	imagedestroy( $image );
	imagedestroy( $source );

}

?>